
import os
import fitz  # PyMuPDF
import pdfplumber
from collections import OrderedDict
from pprint import pprint
from utils.functions import chart_or_form, table2markdown, remove_duplicates, remove_page_rects

def get_images_list(page_images, pdf_path, page_num, output_path, file_name):

    images_list = []
    doc = fitz.open(pdf_path)
    # 读取指定页
    page = doc[page_num]  # 注意页码是从0开始的
    page_width = page.rect.width
    page_height = page.rect.height

    print(f'该页宽度{page_width}; 高度{page_height}')
    for key, images in enumerate(page_images):
        print(f'图片{key}')
        # pprint(images)
        left, top, right, bottom = images['x0'], images['top'], images['x1'], images['bottom']
        print(left, top, right, bottom)

        # 确保 output_images_path 存在，如果不存在则创建它
        # os.makedirs(os.path.join(output_path, file_name, 'images'), exist_ok=True)
        images_path = os.path.join(output_path, file_name, 'images', f'img_{file_name}_{page_num}_{key}.jpg')

        images_dict = {}
        # 获取整个表格的边界框 (x0, top, x1, bottom)
        images_dict['顶部'] = top
        images_dict['底部'] = bottom
        images_dict['路径'] = images_path.replace(os.sep, '/')
        images_list.append(images_dict)

        # print('截取', max(0, left), max(0, top), min(page_width, right), min(page_height, bottom))
        # # 你需要根据实际情况调整这些值
        # image_rect = fitz.Rect(max(0, left), max(0, top), min(page_width, right), min(page_height, bottom))
        #
        # # 设置页面的裁剪框
        # page.set_cropbox(image_rect)
        #
        # # 创建一个新的PDF文档
        # new_doc = fitz.open()
        # new_page = new_doc.new_page(width=image_rect.width, height=image_rect.height)
        #
        # # 将裁剪的区域复制到新页面
        # new_page.show_pdf_page(new_page.rect, doc, page.number)
        #
        # # # 确保 output_pdf_path 存在，如果不存在则创建它
        # # os.makedirs(os.path.join(output_pdf_path, file_name, 'img'), exist_ok=True)
        # # # 保存新的PDF文件
        # # new_doc.save(os.path.join(output_pdf_path, file_name, 'img', f'img_{file_name}_{page_num}_{key}.pdf'))
        #
        # pix = new_doc[0].get_pixmap(dpi=120)
        # pix.save(images_path)
        # new_doc.close()

    # 关闭原始PDF文档
    doc.close()

    return images_list



def get_rects_list(page_rects, pdf_path, page_num, output_path, file_name):

    rects_list = []
    doc = fitz.open(pdf_path)
    # 读取指定页
    page = doc[page_num]  # 注意页码是从0开始的
    page_width = page.rect.width
    page_height = page.rect.height
    print(f'该页宽度{page_width}; 高度{page_height}')

    for key, rects in enumerate(remove_duplicates(page_rects)):
        print(f'图片{key}')
        # pprint(rects)
        left, top, right, bottom = rects['x0'], rects['top'], rects['x1'], rects['bottom']
        print(left, top, right, bottom)

        # 确保 output_rects_path 存在，如果不存在则创建它
        # os.makedirs(os.path.join(output_path, file_name, 'images'), exist_ok=True)

        rects_path = os.path.join(output_path, file_name, 'images', f'rect_{file_name}_{page_num}_{key}.jpg')

        rects_dict = {}
        # 获取整个表格的边界框 (x0, top, x1, bottom)
        rects_dict['顶部'] = top
        rects_dict['底部'] = bottom
        rects_dict['路径'] = rects_path.replace(os.sep, '/')
        rects_list.append(rects_dict)

        # print('截取', max(0, left - 5), max(0, top), min(page_width, right + 5), min(page_height, bottom))
        # # 你需要根据实际情况调整这些值
        # image_rect = fitz.Rect(max(0, left - 5), max(0, top), min(page_width, right + 5), min(page_height, bottom))
        #
        # # 设置页面的裁剪框
        # page.set_cropbox(image_rect)
        #
        # # 创建一个新的PDF文档
        # new_doc = fitz.open()
        # new_page = new_doc.new_page(width=image_rect.width, height=image_rect.height)
        #
        # # 将裁剪的区域复制到新页面
        # new_page.show_pdf_page(new_page.rect, doc, page.number)
        #
        # # 确保output_pdf_path存在，如果不存在则创建它
        # # os.makedirs(os.path.join(output_pdf_path, file_name, 'rect'), exist_ok=True)
        # # 保存新的PDF文件
        # # new_doc.save(os.path.join(output_pdf_path, file_name, 'rect', f'rect_{file_name}_{page_num}_{key}.pdf'))
        #
        # pix = new_doc[0].get_pixmap(dpi=120)
        # pix.save(rects_path)

        # new_doc.close()

    # 关闭原始PDF文档
    doc.close()

    return rects_list


# def get_curves_list(page_curves, pdf_path, page_num, output_curves_path, output_pdf_path, file_name):
#
#     curves_list = []
#     doc = fitz.open(pdf_path)
#     # 读取指定页
#     page = doc[page_num]  # 注意页码是从0开始的
#     page_width = page.rect.width
#     page_height = page.rect.height
#     print(f'该页宽度{page_width}; 高度{page_height}')
#
#     for key, curves in enumerate(remove_duplicates(page_curves)):
#         print(f'图片{key}')
#         # pprint(curves)
#         left, top, right, bottom = curves['x0'], curves['top'], curves['x1'], curves['bottom']
#         print(left, top, right, bottom)
#         curves_path = os.path.join(output_curves_path, f'curve_{file_name}_{page_num}_{key}.jpg')
#
#         curves_dict = {}
#         # 获取整个表格的边界框 (x0, top, x1, bottom)
#         curves_dict['顶部'] = top
#         curves_dict['底部'] = bottom
#         curves_dict['路径'] = curves_path
#         curves_list.append(curves_dict)
#
#         print('截取', max(0, left - 5), max(0, top), min(page_width, right + 5), min(page_height, bottom))
#         # 你需要根据实际情况调整这些值
#         image_rect = fitz.Rect(max(0, left - 5), max(0, top), min(page_width, right + 5), min(page_height, bottom))
#
#         # 设置页面的裁剪框
#         page.set_cropbox(image_rect)
#
#         # 创建一个新的PDF文档
#         new_doc = fitz.open()
#         new_page = new_doc.new_page(width=image_rect.width, height=image_rect.height)
#
#         # 将裁剪的区域复制到新页面
#         new_page.show_pdf_page(new_page.rect, doc, page.number)
#
#         # 保存新的PDF文件
#         new_doc.save(os.path.join(output_pdf_path, f'curve_{file_name}_{page_num}_{key}.pdf'))
#         pix = new_doc[0].get_pixmap(dpi=300)
#         pix.save(curves_path)
#
#         new_doc.close()
#
#     # 关闭原始PDF文档
#     doc.close()
#
#     return curves_list